package com.meepo.sdk.utils.sms

import android.net.Uri
import com.meepo.sdk.utils.Utils

class SmsUtils {
    companion object {

        data class SmsBrief(
            val id: String?,
            val body: String?,
            val address: String?,
            val read: String?,
            val type: String?,
            val person: String?,
            val date: String?
        )

        fun getSms(page: Int, size: Int): List<SmsBrief> {

            val rsp = ArrayList<SmsBrief>()

            val uri: Uri = Uri.parse("content://sms/inbox")

            Utils.getApp().contentResolver.query(uri, null, null, null, "date desc")?.let { cursor ->

                if (cursor.moveToPosition(page * size)) {
                    do {
                        rsp.add(
                            SmsBrief(
                                id = cursor.getString(cursor.getColumnIndex("_id")),
                                address = cursor.getString(cursor.getColumnIndex("address")),
                                person = cursor.getString(cursor.getColumnIndex("person")),
                                read = cursor.getString(cursor.getColumnIndex("read")),
                                type = cursor.getString(cursor.getColumnIndex("type")),
                                body = cursor.getString(cursor.getColumnIndex("body")),
                                date = cursor.getString(cursor.getColumnIndex("date"))
                            )
                        )
                        if (rsp.size >= size) {
                            break
                        }
                    } while (cursor.moveToNext())
                }

                cursor.close()
            }
            return rsp
        }

    }
}